//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.RuntimeServices.WorkflowOutput
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Workflow.Runtime.Tracking;

    /// <summary>
    /// Serves as the Tracking channel for the Output Tracking Service.
    /// </summary>
    public class OutputTrackingChannel : TrackingChannel
    {        
        /// <summary>
        /// Service Provider used to retrieve a service object.
        /// </summary>
        private IServiceProvider serviceProvider = null;

        /// <summary>
        /// Tracking parameters.
        /// </summary>
        private TrackingParameters parameters = null;
        
        /// <summary>
        /// Initializes a new instance of the OutputTrackingChannel class.
        /// </summary>
        /// <param name="serviceProvider">The service provider object.</param>
        /// <param name="parameters">Tracking parameters.</param>
        public OutputTrackingChannel(IServiceProvider serviceProvider, TrackingParameters parameters)
        {
                 this.serviceProvider = serviceProvider;
                 this.parameters = parameters;
        }

        /// <summary>
        /// InstanceCompletedOrTerminated is called by Tracking runtime to 
        /// indicate that the Workflow instance finished running.
        /// </summary>
        protected override void InstanceCompletedOrTerminated()
        {
        }

        /// <summary>
        /// Implement the TrackingChannel abstract Send method.
        /// </summary>
        /// <param name="record">The TrackingRecord.</param>
        protected override void Send(TrackingRecord record)
        {      
            WorkflowOutputSaveService saveService = serviceProvider.GetWorkflowOutputSaveService();

            if (saveService != null)
            {
                saveService.SaveWorkflowOutputs(serviceProvider, record);
            }
        }
    }
}
